﻿using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XH.SmartParking.IService;
using XH.SmartParking.Entities;

namespace XH.SmartParking.Service
{
    public class AutoService : BaseService, IAutoService
    {
        public AutoService(DbContext context)
            : base(context)
        {
        }

        public IEnumerable<AutoRegister> GetAutoList(string key, int pageSize, int pageIndex, out int totalCount)
        {
            var pResult = QueryPage<AutoRegister, string>(q =>
                                                  string.IsNullOrEmpty(key) ||
                                                  q.AutoLicense.Contains(key),

                                                  pageSize,
                                                  pageIndex,
                                                  order => order.AutoLicense,
                                                  false
                                                  );
            totalCount = pResult.TotalCount;

            return from a in pResult.DataList
                   join lc in Set<BaseLicenseColor>() on a.LicenseColorId equals lc.ColorId
                   join lt in Set<BaseLicenseType>() on a.LicenseType equals lt.TypeId
                   join ac in Set<BaseAutoColor>() on a.AutoColorId equals ac.ColorId
                   join fm in Set<BaseFeeMode>() on a.FeeModeId equals fm.FeeModeId
                   select new AutoRegister
                   {
                       AutoId = a.AutoId,
                       AutoLicense = a.AutoLicense,
                       LicenseColorId = a.LicenseColorId,
                       LicenseColorName = lc.ColorName,
                       LicenseType = a.LicenseType,
                       LicenseTypeName = lt.TypeName,
                       AutoColorId = a.AutoColorId,
                       AutoColorName = ac.ColorName,
                       FeeModeId = a.FeeModeId,
                       FeeModeName = fm.FeeModeName,
                       Description = a.Description,
                       ValidCount = a.ValidCount,
                       ValidEndTime = a.ValidEndTime,
                       ValidStartTime = a.ValidStartTime,
                       State = a.State
                   };
        }

        public AutoRegister GetAutoByLicense(string license)
        {
            var auto = Query<AutoRegister>(a => a.AutoLicense == license)
                .FirstOrDefault();
            if (auto == null) return new AutoRegister();

            return auto;
        }
    }
}
